/**
 * Created by MyPC on 2017/6/18.
 */
let mysql = require('../../common/connection');
let dao = {};

dao.listByAll = function () {
  let sql = 'SELECT id,`parent_id` AS parentId,`names`,`has_children` AS hasChildren,`counts`,`is_public` AS isPublic FROM `t_b_themes` WHERE `delete_time` IS NULL order by parent_id asc';
  let arr = [];
  return mysql.query(sql, arr);
}
// 新增
dao.save = function (params) {
  let sql = 'INSERT INTO `t_b_themes`(`parent_id`,`names`,`has_children`,`is_public`,`remarks`,`operator_id`) VALUES(?,?,?,?,?,?)';
  var arr = [];
  arr.push(params.parentId);
  arr.push(params.names);
  arr.push(params.hasChildren);
  arr.push(params.isPublic);
  arr.push(params.remarks);
  arr.push(params.operatorId);
  return mysql.query(sql, arr);
}
// 修改
dao.updateById = function (params) {
  let sql = 'UPDATE t_b_themes SET `update_time`=NOW()';
  let arr = [];
  if(params.parentId !== undefined){
    sql += ',`parent_id`=?';
    arr.push(params.parentId);
  }
  if(params.names !== undefined){
    sql += ',`names`=?';
    arr.push(params.names);
  }
  if(params.hasChildren !== undefined){
    sql += ',`has_children`=?';
    arr.push(params.hasChildren);
  }
  if(params.isPublic !== undefined){
    sql += ',`is_public`=?';
    arr.push(params.isPublic);
  }
  if(params.remarks !== undefined){
    sql += ',`remarks`=?';
    arr.push(params.remarks);
  }
  sql += ' where `id`=?';
  arr.push(params.id);
  return mysql.query(sql, arr);
}
// 删除
dao.removeById = function (params) {
  let sql = 'UPDATE t_b_themes SET `delete_time`=NOW() where `id`=?';
  let arr = [];
  arr.push(params.id);
  return mysql.query(sql, arr);
}
module.exports = dao;
